import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_recharge.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/utils/utils_time.dart';
import 'package:flutter_09_game/widgets/button/debounce_gesture_detector.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/widgets/stepper/stepper_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class WidgetRechargeDetail extends StatelessWidget {
  const WidgetRechargeDetail({
    super.key,
    required this.record,
    this.isWithdraw = false,
  });

  final ModelRecordRecharge record;
  final bool isWithdraw;

  @override
  Widget build(BuildContext context) {
    // record.status = 2;
    clog('dlajksdkl   ${record.toJson()}');
    return _buildContent();
  }

  /// 构建内容区域
  Widget _buildContent() {
    return Container(
      padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
      child: Column(
        children: [
          // 如果是提现，显示进度条
          if (isWithdraw) ...[
            _buildWithdrawStepper(),
            24.verticalSpaceFromWidth,
          ],
          _buildSuccessStatus(),
          24.verticalSpaceFromWidth,
          _buildDetailInfo(),
        ],
      ),
    );
  }

  /// 构建提现进度条
  Widget _buildWithdrawStepper() {
    // 根据状态确定当前步骤
    int currentStep = _getCurrentStep();

    List<StepperItem> steps = [
      StepperItem(
        title: LocaleKeys.withdrawCommission.tr,
        time: UtilsTime.depositFormatTime(record.createdAt),
      ),

      StepperItem(
        title: LocaleKeys.processing.tr,
        time: UtilsTime.depositFormatTime(record.createdAt),
      ),
    ];
    if (currentStep == 1) {
      steps.add(const StepperItem(
        title: '',
        time: null,
      ));
    } else if (currentStep == 2) {
      steps.add(StepperItem(
        title: LocaleKeys.withdrawSuccess.tr,
        time: UtilsTime.depositFormatTime(record.processAt),
      ));
    } else if (currentStep == 3) {
      steps.add(StepperItem(
        title: LocaleKeys.withdrawFailed.tr,
        time: UtilsTime.depositFormatTime(record.processAt),
      ));
    }

    return StepperWidget(
      steps: steps,
      currentStep: currentStep,
    );
  }

  /// 根据状态获取当前步骤
  int _getCurrentStep() {
    switch (record.status) {
      case 1:
      case 2:
        return 1; // 处理中
      case 3:
        return 2; // 完成
      case 4:
      case 5:
        return 3; // 失败，显示为第一步
      default:
        return 0;
    }
  }

  /// 构建成功状态展示
  Widget _buildSuccessStatus() {
    return Column(
      children: [
        // 状态图标
        AutoImage(
          _getStatusIcon(),
          width: 80.w,
          height: 80.w,
        ),
        24.verticalSpaceFromWidth,
        // 状态文本
        Text(
          _getStatusText(),
          style: TextStyle(
            color: AppColor.textPrimary.withOpacity(0.94),
            fontSize: 14.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
        8.verticalSpaceFromWidth,
        // 金额
        Text(
          '+${record.amount?.formatWallet() ?? ''} ${record.symbol ?? ''}',
          style: TextStyle(
            color: AppColor.primary,
            fontSize: 24.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
      ],
    );
  }

  /// 获取状态图标
  String _getStatusIcon() {
    if (isWithdraw) {
      switch (record.status) {
        case 3:
          return Assets.commonRechargeSuccess; // 提现成功
        case 1:
        case 2:
          return Assets.commonWaitingArrival; // 等待到账
        case 4:
        case 5:
          return Assets.commonWithdrawFailed; // 提现失败
        default:
          return Assets.commonWaitingArrival;
      }
    } else {
      return Assets.commonRechargeSuccess; // 充值成功
    }
  }

  /// 获取状态文本
  String _getStatusText() {
    if (isWithdraw) {
      switch (record.status) {
        case 3:
          return LocaleKeys.withdrawSuccess.tr; // 提现成功
        case 1:
        case 2:
          return LocaleKeys.waitingForArrival.tr; // 等待到账
        case 4:
        case 5:
          return LocaleKeys.withdrawFailed.tr; // 提现失败
        default:
          return LocaleKeys.waitingForArrival.tr;
      }
    } else {
      return LocaleKeys.rechargeSuccess.tr; // 充值成功
    }
  }

  /// 构建详情信息
  Widget _buildDetailInfo() {
    return Column(
      children: [
        // 账户地址信息
        _buildInfoRow(
          isWithdraw
              ? LocaleKeys.withdrawAccount.tr
              : LocaleKeys.rechargeAccount.tr,
          isWithdraw
              ? (record.withdrawAddress ?? '')
              : (record.rechargeAddress ?? ''),
        ),
        12.verticalSpaceFromWidth,
        // 货币信息
        _buildInfoRow(
          isWithdraw
              ? LocaleKeys.withdrawCurrency.tr
              : LocaleKeys.rechargeCurrency.tr,
          record.withdrawAddress,
          rightWidget: Row(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              AutoImage(
                UtilsOther().getAmountTagIcon(record.symbol),
                width: 16.w,
                height: 16.w,
              ),
              4.horizontalSpace,
              Text(
                record.symbol ?? '',
                style: TextStyle(
                  color: AppColor.textPrimary.withOpacity(0.7),
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
        ),
        12.verticalSpaceFromWidth,
        // 时间信息
        _buildInfoRow(
          isWithdraw ? LocaleKeys.withdrawTime.tr : LocaleKeys.rechargeTime.tr,
          UtilsTime.depositFormatTime(record.createdAt),
        ),
        12.verticalSpaceFromWidth,
        // 订单编号
        _buildInfoRow(
          LocaleKeys.orderNumber.tr,

          null,
          rightWidget: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text(
                record.identity ?? '',
                style: TextStyle(
                  color: AppColor.textPrimary.withOpacity(0.7),
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w400,
                ),
              ),
              4.horizontalSpace,
              DebounceGestureDetector(
                onTap: () => UtilsOther.copyMemberId(record.identity ?? ''),
                child: AutoImage(
                  Assets.mineCopy,
                  width: 16.w,
                  height: 16.w,
                  color: AppColor.textPrimary.withOpacity(0.7),
                ),
              ),
            ],
          ),
        ),

        Visibility(
            visible: record.status == 5 || record.status == 4,
            child: _buildInfoRow(
         LocaleKeys.rejectionReason.tr,
          record.remark ??'',
        ))
      ],
    );
  }

  /// 构建信息行
  Widget _buildInfoRow(String label, String? value, {Widget? rightWidget}) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 4.w),
      child: Row(
        children: [
          SizedBox(
            width: 76.w,
            child: Text(
              label,
              style: TextStyle(
                color: AppColor.textPrimary.withOpacity(0.94),
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          12.horizontalSpace,
          Expanded(
            child: rightWidget ??
                Text(
                  value ?? '',
                  textAlign: TextAlign.end,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: AppColor.textPrimary.withOpacity(0.7),
                    fontSize: 12.sp,
                    fontWeight: FontWeight.w400,
                  ),
                ),
          ),
        ],
      ),
    );
  }

}
